package com.fenghongzhang.welcometest;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {
    private ViewPager vp;

    private TwoFragment twoFragment;
    private List<Fragment> list;
    private LinearLayout ll;

    private int index = 0;
    private Timer timer;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        vp = (ViewPager) findViewById(R.id.vp);
        ll = (LinearLayout) findViewById(R.id.ll);


        //第一走, 有欢迎页,第二次就没有了
        SharedPreferences welcome = getSharedPreferences("welcome", MODE_PRIVATE);
        boolean isOpen = welcome.getBoolean("isOpen", false);
        if (isOpen){
            Intent intent = new Intent(this, Main2Activity.class);
            startActivity(intent);

            //跳转以后,下面的代码还执行吗?
            finish();
            return;

        }else {
            SharedPreferences.Editor edit = welcome.edit();
            edit.putBoolean("isOpen",true);
            edit.commit();
        }





        list = new ArrayList<>();
        list.add(new OneFragment());
        list.add(new OneFragment());
        list.add(new OneFragment());
        list.add(new OneFragment());
         twoFragment = new TwoFragment();
        list.add(twoFragment);

        MyAdapter myAdapter = new MyAdapter(getSupportFragmentManager(),list);
        vp.setAdapter(myAdapter);


        //和vp的下标对应,去修改结合中图片
        final List<ImageView> imgList = new ArrayList<>();
        //把小圆点也用集合包起来.
        for (int i = 0; i < list.size(); i++) {
            ImageView imageView = new ImageView(this);
            if(i == 0){
                imageView.setImageResource(R.drawable.circle_t);
            }else{
                imageView.setImageResource(R.drawable.circle_f);
            }
            imgList.add(imageView);
            ll.addView(imageView);
        }


        //定时器, 来自动翻页
        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        vp.setCurrentItem(index);
                        index++;
                        if(index > list.size() -1){
                            timer.cancel();
                        }
                    }
                });

            }
        },0,1000);


        //vp的翻页监听
        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                /**
                 * 如果当前是第二页,那么我就通知第二个fragment开始倒计时
                 */
                if(position == list.size()-1){
                    twoFragment.handler.sendEmptyMessage(110);
                }

                /**
                 * 修改集合中的图片,如果是选中,就改, 不是就花,就改成默认颜色.
                 */
                for (int i = 0; i < imgList.size(); i++) {
                    if(position == i){
                        imgList.get(i).setImageResource(R.drawable.circle_t);
                    }else {
                        imgList.get(i).setImageResource(R.drawable.circle_f);
                    }
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }
}
